/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.ta.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import com.thinkgem.jeesite.modules.ta.entity.StWeihu;
import com.thinkgem.jeesite.modules.ta.service.LiuchengService;
import com.thinkgem.jeesite.modules.ta.service.StWeihuService;

/**
 * 附件Controller
 * 
 * @author TTAO
 * @version 2017-06-05
 */
@Controller
@RequestMapping(value = "${adminPath}/ta/stWeihu")
public class StWeihuController extends BaseController {

	@Autowired
	private StWeihuService stWeihuService;
	@Autowired
	private LiuchengService liuchengService;

	@ModelAttribute
	public StWeihu get(@RequestParam(required = false) String id) {
		StWeihu entity = null;
		if (StringUtils.isNotBlank(id)) {
			entity = stWeihuService.get(id);
		}
		if (entity == null) {
			entity = new StWeihu();
		}
		return entity;
	}

	@RequiresPermissions("ta:stWeihu:view")
	@RequestMapping(value = { "list", "" })
	public String list(StWeihu stWeihu, HttpServletRequest request,
			HttpServletResponse response, Model model) {
		Page<StWeihu> page = stWeihuService.findPage(new Page<StWeihu>(request,
				response), stWeihu);
		User user = UserUtils.getUser();
		if(user == null){
			return "modules/sys/sysLogin";
		}
		String userId = user.getNo();
		List<String> ls = liuchengService.findList(userId);
		model.addAttribute("page", page);
		model.addAttribute("ls", ls);
		return "modules/ta/stWeihuList";
	}

	@RequestMapping(value = "selectInfo")
	public String selectInfo(String iid) {
		return "redirect:" + Global.getAdminPath()
				+ "/ta/stDynamicResource/list?iid=" + iid;
	}

	@RequiresPermissions("ta:stWeihu:view")
	@RequestMapping(value = "form")
	public String form(StWeihu stWeihu, Model model) {
		model.addAttribute("stWeihu", stWeihu);
		return "modules/ta/stWeihuForm";
	}

	@RequiresPermissions("ta:stWeihu:edit")
	@RequestMapping(value = "save")
	public String save(StWeihu stWeihu, Model model,
			RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, stWeihu)) {
			return form(stWeihu, model);
		}
		stWeihuService.save(stWeihu);
		addMessage(redirectAttributes, "保存附件成功");
		return "redirect:" + Global.getAdminPath() + "/ta/stWeihu/?repage";
	}

	@RequiresPermissions("ta:stWeihu:edit")
	@RequestMapping(value = "delete")
	public String delete(StWeihu stWeihu, RedirectAttributes redirectAttributes) {
		stWeihuService.delete(stWeihu);
		addMessage(redirectAttributes, "删除附件成功");
		return "redirect:" + Global.getAdminPath() + "/ta/stWeihu/?repage";
	}

}